/** 
 * 授权订阅消息
 * @param {Array} tmplIds 需要订阅的消息模板的id的集合，一次调用最多可订阅3条消息
 */
export function setSubscribeMessage(tmplIds) {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			withSubscriptions: true,
			success(alwaysRes) {
				if (!alwaysRes.subscriptionsSetting.mainSwitch) {
					uni.showModal({
						title: '检测到您未开启订阅消息通知权限，是否去设置？',
						success(res) {
							if (res.confirm) {
								uni.openSetting({
									success(res) {
										if (res.errMsg == 'openSetting:ok') {
											console.log('开启权限成功', res)
											resolve(res)
										} else {
											console.log('开启权限成功', res)
											resolve(res)
										}
									},
									fail(res) {
										console.log('开启权限失败', res)
										reject(res)
									},
								})
							} else if (res.cancel) {
								console.log('拒绝开启开启权限', res)
								reject(res)
							}
						},
					})
				} else {
					uni.requestSubscribeMessage({
						tmplIds: tmplIds,
						success(res) {
							console.log("授权订阅成功", res)
							resolve(res)
						},
						fail(res) {
							console.log('授权订阅失败', res)
							reject(res)
						},
						complete(res) {
							resolve(res)
						},
					})
				}
			},
			fail(res) {
				console.log('获取权限失败', res)
				reject(res)
			}
		})
	})
}